package Word_Frequency;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TEST {

    public void Task(String document_) throws IOException {
        //String document_ = S;
        // 多文档任务
        List<String> documents = new ArrayList<>();
        documents.add(document_);
        String stopWordsFilePath = "停用词表.txt";
        WordFrequency wordFrequency = new WordFrequency(stopWordsFilePath);
        TextRankCalculator textRankCalculator = new TextRankCalculator(wordFrequency);

        for (String document : documents) {
            // 输出词频统计结果
            Map<String, Integer> wordFreq = wordFrequency.getWordFrequency(document);
            //System.out.println("Document: " + document);
            System.out.println("Word Frequency (sorted by frequency):");
            wordFreq.entrySet().stream()
                    .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                    .forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
            System.out.println();

            // 输出TextRank结果并标准化到1-100范围
            Map<String, Double> textRank = textRankCalculator.calculateTextRank(document);
            Map<String, Integer> normalizedTextRank = normalizeScores(textRank);
            System.out.println("Top TextRank Words (1-100):");
            normalizedTextRank.entrySet().stream()
                    .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                    .forEach(entry -> System.out.println("Word: " + entry.getKey() + ", TextRank: " + entry.getValue()));
            System.out.println();

            // 转换为JSON格式
            //Gson gson = new GsonBuilder().setPrettyPrinting().create();
            //String jsonOutput = gson.toJson(normalizedTextRank);
            //测试输出
            //System.out.println("JSON Output:");
            //System.out.println(jsonOutput);

            // 保存词频结果到txt文件
            saveWordFrequencyToFile(wordFreq, "ruoyi-hzhtest/src/main/resources/files/word_frequency.txt");
            // 保存TextRank结果到txt文件
            saveTextRankToFile(normalizedTextRank, "ruoyi-hzhtest/src/main/resources/files/textrank.txt");

            System.out.println();
        }

    }

    public static Map<String, Integer> normalizeScores(Map<String, Double> scores) {
        Map<String, Integer> normalizedScores = new HashMap<>();
        double minScore = scores.values().stream().min(Double::compare).orElse(0.0);
        double maxScore = scores.values().stream().max(Double::compare).orElse(1.0);
        for (Map.Entry<String, Double> entry : scores.entrySet()) {
            int normalizedScore = (int) ((entry.getValue() - minScore) / (maxScore - minScore) * 49) + 1;
            normalizedScores.put(entry.getKey(), normalizedScore);
        }
        return normalizedScores;
    }

    public static void saveWordFrequencyToFile(Map<String, Integer> wordFrequency, String fileName) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        wordFrequency.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .forEach(entry -> {
                    try {
                        writer.write(entry.getKey() + " " + entry.getValue());
                        writer.newLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
        writer.close();
    }

    public static void saveTextRankToFile(Map<String, Integer> textRank, String fileName) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        textRank.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .forEach(entry -> {
                    try {
                        writer.write(entry.getKey() + " " + entry.getValue());
                        writer.newLine();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
        writer.close();
    }

    // 返回词频结果
    public static String[][] getSortedWordFrequency(Map<String, Integer> wordFrequency) {
        return wordFrequency.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .map(entry -> new String[]{entry.getKey(), entry.getValue().toString()})
                .toArray(String[][]::new);
    }

    // 返回TextRank结果
    public static String[][] getSortedTextRank(Map<String, Integer> textRank) {
        return textRank.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .map(entry -> new String[]{entry.getKey(), entry.getValue().toString()})
                .toArray(String[][]::new);
    }
}

